const utils=require("utils.js")
Page({
    data:{
        meters:0,
        seconds:0,
        latitude:0,
        longitude:0,
        running:false,
        interval:1000,
        markers:[],
        save:[{licen:0,sj:0,luj:{},dian:[]}],
        xiaodian:[]  
    },
    curLocation(){
        wx.getLocation({
          type:'gcj02'
        }).then(res=>{
            let {latitude,longitude}=res 
            this.setData({
                latitude,longitude
            })
        })
    },
    onLoad(options){
        this.curLocation()
        setInterval(this.record,this.data.interval)
            wx.getStorage({
                key:'luj'
            }).then(res=>{
                this.setData({
                    save:res.data[0],
                    meters:res.data[1],
                    seconds:res.data[2],
                    markers:res.data[3],
                    xiaodian:res.data[3]
                })
        })
    },
    test(){
        let meters=utils.getDistance(23.383101,113.449479,23.38703,113.446121)
        this.setData({
            meters
        })
    },
    run(){
        this.setData({
            running:!this.data.running
        })
    },
    record(){
        if(!this.data.running){
            return
        }
        this.setData({
            seconds:this.data.seconds+this.data.interval/1000
        })
        wx.getLocation({
          type:'gjc02'
        }).then(res=>{
            let newMarker={
                latitude:res.latitude,
                longitude:res.longitude,
                iconPath:'redPoint.png',
                width:10,
                height:10
            }
            let pace=0
            let markers=this.data.markers
            if(this.data.markers.length>0){
                let lastMarker=this.data.markers.slice(-1)[0]
                console.log(lastMarker) 
                console.log(newMarker)
                pace=utils.getDistance(lastMarker.latitude,lastMarker.longitude,newMarker.latitude,newMarker.longitude)
                if(pace>15){
                    markers.push(newMarker)
                }else{
                    pace=0
                }
            }else{
                markers.push(newMarker)
            }
            this.setData({
                latitude:res.latitude,longitude:res.longitude,markers,meters:this.data.meters+pace,xiaodian:markers
            })
        })
    },
    clear(){
        this.setData({
            markers:[],seconds:0,meters:0,save:[{licen:0,sj:0,luj:{}}]
        })
    },
    save(){
        let meters=this.data.meters
        let seconds=this.data.seconds
        let markers=this.data.markers
        let xiaodian=markers
        let save=[{licen:meters,sj:seconds,dian:markers}]
        console.log(save)
        this.setData({
            save,meters,seconds,markers,xiaodian
        })
        console.log(xiaodian)
    },
    playback(){
        let save=this.data.save
        let xiaodian=this.data.xiaodian
        let i=0
        console.log(xiaodian)
        this.setData({
            markers:[]
        })
        let that=this
        let jsq=setInterval(function(){
            if(i<=xiaodian.length-1){
                let markers=that.data.markers
                markers.push(xiaodian[i])
                that.setData({
                    markers
                })
                i++
            }else{
                clearInterval(jsq)
            }
        },1000/20)
    },
    onUnload(){
        this.onHide()
    },
    onHide(){
        let store=[this.data.save,this.data.meters,this.data.seconds,this.data.markers]
        wx.setStorage({
            data:store,
            key:'luj'
        })
    }
})